package com.example.hehecat.yuejian1.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ClubInfoContract;
import com.example.hehecat.yuejian1.mvp.model.ClubInfoModel;
import com.example.hehecat.yuejian1.mvp.model.api.Api;
import com.example.hehecat.yuejian1.mvp.model.bean.ClubBean;
import com.example.hehecat.yuejian1.mvp.model.bean.ClubLessonBean;
import com.example.hehecat.yuejian1.mvp.presenter.ClubInfoPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ClubLessonListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;
import com.ldoublem.thumbUplib.ThumbUpView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;

public class ClubInfoFragment extends SupportFragment implements ClubInfoContract.View {
    Unbinder unbinder;
    @BindView(R.id.clubinfo_return)
    ImageView clubinfoReturn;
    @BindView(R.id.TextView)
    android.widget.TextView TextView;
    @BindView(R.id.club_share)
    Button clubShare;
    @BindView(R.id.club_tpv)
    ThumbUpView clubTpv;
    @BindView(R.id.club_img)
    ImageView clubImg;
    @BindView(R.id.title)
    AppCompatTextView title;
    @BindView(R.id.clubinfo_intro)
    AppCompatTextView clubinfoIntro;
    @BindView(R.id.club_lessons)
    RecyclerView clubLessons;

    int club_id;
    int user_id;
    ClubInfoPresenter presenter;
    ClubLessonListAdapter adapter;


    @OnClick(R.id.clubinfo_return)
    void clubInfoReturn(){
        ClubFragment fragment = new ClubFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, fragment, null)
                .commit();
    }

    @OnClick(R.id.club_share)
    void clubShare() {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, clubinfoIntro.getText());
        startActivity(Intent.createChooser(textIntent, "分享"));
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_club_info, container, false);
        unbinder = ButterKnife.bind(this, view);


        if (getArguments() != null) {
            club_id = getArguments().getInt("club_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();

        presenter = new ClubInfoPresenter(this);
        presenter.updateAdapterData(club_id);

        //点赞
        isClubCollect();
        clubTpv.setCracksColor(Color.rgb(22, 33, 44));
        clubTpv.setFillColor(Color.rgb(11, 200, 77));
        clubTpv.setEdgeColor(Color.rgb(33, 3, 219));
        clubTpv.setOnThumbUp(like -> collectClub());

        //创建布局管理
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        clubLessons.setLayoutManager(layoutManager);
        clubLessons.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ClubLessonListAdapter(R.layout.item_list, getContext());
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setNotDoAnimationCount(2);

        //获取详情
        ClubInfoModel model = new ClubInfoModel();
        model.getClub(club_id, new Subscriber<ClubBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ClubBean clubBean) {

                clubinfoIntro.setText(clubBean.getIntro());
                Picasso.with(getContext()).load(Api.IMG_URL + clubBean.getClubimg()).into(clubImg);
            }
        });
        
        //给RecyclerView设置适配器
        clubLessons.setAdapter(adapter);

        return view;

    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setCollect() {
        clubTpv.setLike();
    }

    @Override
    public void setUnCollect() {
        clubTpv.setUnlike();
    }

    @Override
    public void updateAdapterData(List<ClubLessonBean> data) {
       adapter.addData(data);
    }

    @Override
    public void collectClub() {
        presenter.collectClub(user_id,club_id);
    }

    @Override
    public void isClubCollect() {
        presenter.isCollected(club_id,user_id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static ClubInfoFragment newInstance(int club_id) {
        ClubInfoFragment fragment = new ClubInfoFragment();
        Bundle args = new Bundle();
        args.putInt("club_id", club_id);
        fragment.setArguments(args);
        return fragment;
    }

}
